import OutputConfig from './config';
import * as Plugins from './plugins';
import {registerEvents} from './events';
import G6 from '@antv/g6';

/**
 * 默认布局
 * @param domContainer
 * @param plugins
 * @returns {*}
 */
export function initModel(domElement: HTMLElement, TopoConfig = {}, plugins = ['tooltip', 'toolbar']) {
    const config = ConfigFactory(domElement, {
        type: 'model',
        ...TopoConfig
    }, plugins);
    let graph = new G6.Graph(config);
    registerEnvironment(graph);
    return graph;
}

export function ConfigFactory(domContainer: HTMLElement, config: any = {
    type: 'model',
    history: '',
}, plugins: Array<any>) {
    let G6Config;
    G6Config = OutputConfig(domContainer);

    AppendCommon(G6Config);

    for(const plugin of plugins) {
        if(Plugins[plugin]) {
            G6Config.plugins.push(Plugins[plugin]);
        }
    }

    return G6Config;
}

function AppendCommon(config: any) {
    config.defaultEdge = {
        type: 'ref',
        labelCfg: {
            style: {},
            refY: 10,
            autoRotate: true
        },
    };
}

/**
 * 初始化一些
 */
function registerEnvironment(graph) {
    window.graph = graph;
    // nodes
    registerEvents(graph);

    graph.fitView();
}
